//go:build linux

package util

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

func GetOSPrettyName() string {
	switch runtime.GOOS {
	case "freebsd":
		fallthrough
	case "openbsd":
		fallthrough
	case "android":
		fallthrough
	case "linux":
		data, err := os.ReadFile("/etc/os-release")
		if err != nil {
			return string("获取异常")
		}
		osInfos := strings.Split(string(data), "\n")
		for _, line := range osInfos {
			if strings.HasPrefix(line, "PRETTY_NAME") {
				return strings.Trim(strings.TrimPrefix(line, "PRETTY_NAME="), "\"")
			}
		}
		return string("获取失败")
	case "darwin":
		cmd := exec.Command("sw_vers")
		output, err := cmd.Output()
		if err != nil {
			return string("获取异常")
		}
		lines := strings.Split(string(output), "\n")
		var version string
		for _, line := range lines {
			if strings.HasPrefix(line, "ProductVersion") {
				version = strings.TrimSpace(strings.Split(line, ":")[1])
			}
		}
		return version
	default:
		return string("暂不支持")
	}
}

func GetInterfaceDescriptionByName(interfaceName string) string {
	devicePath := filepath.Join("/sys/class/net", interfaceName, "device")
	if _, err := os.Stat(devicePath); os.IsNotExist(err) {
		return ""
	}

	vendorPath := filepath.Join(devicePath, "vendor")
	vendor, err := os.ReadFile(vendorPath)
	if err == nil {
		return string(vendor)
	}

	return ""
}

func GetInterfaceDescriptionByMAC(interfaceName string) string {
	return ""
}

func GetConfigDirPath() string {
	exePath, err := os.Executable()
	if err != nil {
		fmt.Printf("Failed to get executable path: %v\n", err)
	}
	configDirPath := filepath.Dir(exePath) + "/" + "config"
	return configDirPath
}

func GetNewLine() string {
	return "\n"
}
